import Vue from 'vue'
import VueRouter from 'vue-router'
import LayOut from '@/views/layout/Index'
import Home from '@/views/layout/Home'
import Cart from '@/views/layout/Cart'
import My from '@/views/layout/My'
import Category from '@/views/layout/Category'
// import MyOrder from '@/views/order/MyOrder.vue'
// import OrderCheck from '@/views/order/OrderCheck.vue'
// import ProductDetail from '@/views/product/ProductDetail.vue'
// import ProductSearch from '@/views/product/ProductSearch.vue'
// import ProductComment from '@/views/product/ProductComment.vue'
// import ProductList from '@/views/product/ProductList.vue'
// import Login from '@/views/login/Login.vue'
// import AddressList from '@/views/address/AddressList.vue'
// import AddAddress from '@/views/address/AddAddress.vue'
// import EditAddress from '@/views/address/EditAddress.vue'
// import OrderDetail from '@/views/order/OrderDetail.vue'
import { getAuthToken } from '@/utils/localstorage'
import { Dialog } from 'vant'

// 路由懒加载，按需加载，不经常访问的页面可以使用路由懒加载
const Login = () => import('@/views/login/Login.vue')
const AddressList = () => import('@/views/address/AddressList.vue')
const AddAddress = () => import('@/views/address/AddAddress.vue')
const EditAddress = () => import('@/views/address/EditAddress.vue')
const ProductDetail = () => import('@/views/product/ProductDetail.vue')
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/pages/home'
    },
    {
      path: '/pages',
      component: LayOut,
      children: [
        {
          path: 'category/index',
          name: 'category',
          component: Category
        },
        {
          path: 'home',
          name: 'home',
          component: Home
        },
        {
          path: 'cart/index',
          name: 'cart',
          component: Cart
        },
        {
          path: 'my/index',
          name: 'my',
          component: My
        }
      ]
    },
    {
      path: '/pages/order/index',
      component: () => import('@/views/order/MyOrder.vue')
    },
    {
      path: '/pages/orderCheck',
      component: () => import('@/views/order/OrderCheck.vue')
    },
    {
      path: '/pages/order/detail',
      component: () => import('@/views/order/OrderDetail.vue')
    },
    {
      path: '/pages/goods/detail',
      name: 'goods_detail',
      component: ProductDetail
    },
    {
      path: '/pages/goods/list',
      name: 'goods_list',
      component: () => import('@/views/product/ProductList.vue')
    },
    {
      path: '/pages/search/index',
      name: 'search',
      component: () => import('@/views/product/ProductSearch.vue')
    },
    {
      path: '/pages/comment/index',
      name: 'comment',
      component: () => import('@/views/product/ProductComment.vue')
    },
    {
      path: '/pages/user/login',
      name: 'login',
      component: Login
    },
    {
      path: '/pages/address/index',
      component: AddressList
    },
    {
      path: '/pages/address/create',
      component: AddAddress
    },
    {
      path: '/pages/address/edit',
      component: EditAddress
    }
  ]
})

// 配置了name的都是白名单
router.beforeEach((to, from, next) => {
  // 白名单，放行
  if (to.name) {
    next()
  } else if (getAuthToken()) {
    // 有权限，放行
    next()
  } else {
    Dialog.confirm({
      title: '温馨提示',
      message: '您还没有登录喔',
      confirmButtonText: '去登陆',
      cancelButtonText: '再逛逛',
      confirmButtonColor: '#258dff'
    }).then(() => {
      router.push('/pages/user/login')
    }).catch((err) => {
      console.log(err)
    })
  }
})

export default router
